//
//  EPAlertPopViewController.swift
//  EPHome
//
//  Created by liujianlong on 2022/8/15.
//

import UIKit
enum EPAlertPopViewControllerType : Int
{
    case withCancel = 1
    case  onlyOK = 2
    case input = 3
}

class EPAlertPopViewController: EPAlertViewController {
    @IBOutlet weak var titleLbl: UILabel!
    @IBOutlet weak var content: UILabel!
    @IBOutlet weak var enter: UIButton!
    @IBOutlet weak var cancel: UIButton!
    @IBOutlet var enterTop: NSLayoutConstraint!
    @IBOutlet var enterCenter: NSLayoutConstraint!
    @IBOutlet var enterLeft: NSLayoutConstraint!
    @IBOutlet var input: UITextField!
    var type:EPAlertPopViewControllerType = .withCancel
    var block:((_ tag:Int, _ text:String?)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        switch self.type {
        case .withCancel:
            self.enterLeft.isActive = true
            self.enterCenter.isActive = false
            self.enter.isHidden = false
            self.cancel.isHidden = false
        case .onlyOK:
            self.enterLeft.isActive = false
            self.enterCenter.isActive = true
            self.enter.isHidden = false
            self.cancel.isHidden = true
        case .input:
            self.enterLeft.isActive = true
            self.enterCenter.isActive = false
            self.enterTop.constant = 44
            self.enter.isHidden = false
            self.cancel.isHidden = false
            self.input.isHidden = false
            self.content.isHidden = true
        }
    }
    
    /// tag = 201 enterbtn
    /// tag = 202 cancelbtn
    /// tab = 203 okbtn
    @IBAction func actionForBtn(_ sender: UIButton) {
        self.dismiss(animated: false)
        if sender.tag != 202{
            self.block?(sender.tag, self.type == .input ? self.input.text! : "")
        }
    }
    
    class func pop(_ title:String, _ content:String, _ type:EPAlertPopViewControllerType ,_ block:((_ tag:Int, _ text:String?)->Void)?=nil){
        let vc = EPAlertPopViewController()
        vc.type = type
        vc.titleLbl.text = title
        if vc.type == .input{
            vc.input.placeholder = content
        }else{
            vc.content.text = content
            vc.content.textAlignment = .center
        }
        vc.block = block
        vc.currentVC().present(vc, animated: false)
    }
    
}
